type int = number;
//class Arithmetic

    export  class Arithmetic
    {
       

        /* internal  */static isStart(arg1: string, arg2: int): Boolean
        {
            return arg2 <= 0 || arg1.charAt((arg2 - 1)) == "(";
        }

        /* internal  */static isOperator(arg1: string, arg2: int): Boolean
        {
            if (arg2 >= arg1.length || arg2 < 0) 
            {
                return false;
            }
            if (Arithmetic.isStart(arg1, arg2)) 
            {
                return false;
            }
            var loc1=arg1.charAt(arg2);
            return loc1 == "+" || loc1 == "-" && !Arithmetic.isOperator(arg1, (arg2 - 1)) || loc1 == "*" || loc1 == "/";
        }

        /* internal  */static isNumber(arg1: string, arg2: int): Boolean
        {
            if (arg2 >= arg1.length || arg2 < 0) 
            {
                return false;
            }
            var loc1=arg1.charAt(arg2);
            return loc1 >= "0" && loc1 <= "9" || loc1 == "." || loc1 == "-" && (Arithmetic.isOperator(arg1, (arg2 - 1)) || Arithmetic.isStart(arg1, arg2));
        }

        /* internal  */static getPriority(arg1: string): int
        {
            var loc1=arg1;
            switch (loc1) 
            {
                case ")":
                {
                    return 3;
                }
                case "*": case "/":
                {
                    return 2;
                }
                case "+": case "-":
                {
                    return 1;
                }
                case "(":
                {
                    return -1;
                }
                default:
                {
                    return 0;
                }
            }
        }

        /* internal  */static getValue(arg1: string, arg2: number, arg3: number): number
        {
            var loc1=arg1;
            switch (loc1) 
            {
                case "*":
                {
                    return arg3 * arg2;
                }
                case "/":
                {
                    return arg3 / arg2;
                }
                case "-":
                {
                    return arg3 - arg2;
                }
                case "+":
                {
                    return arg3 + arg2;
                }
            }
            return NaN;
        }

        public static exec(arg1: string): number
        {
            var loc3=null;
            var loc4=NaN;
            var loc5=NaN;
            var loc7=null;
            var loc8=null;
            var loc1=[];
            var loc2=[];
            var loc6=0;
            while (loc6 < arg1.length) 
            {
                if ((loc7 = arg1.charAt(loc6)) == " ") 
                {
                    ++loc6;
                }
                if (loc7 == "(") 
                {
                    loc1.push(loc7);
                    ++loc6;
                    continue;
                }
                if (loc7 == ")") 
                {
                    loc3 = loc1.pop();
                    while (loc3 != "(") 
                    {
                        loc4 = loc2.pop();
                        loc5 = loc2.pop();
                        loc2.push(Arithmetic.getValue(loc3, loc4, loc5));
                        loc3 = loc1.pop();
                    }
                    ++loc6;
                    continue;
                }
                if (Arithmetic.isOperator(arg1, loc6)) 
                {
                    while (Arithmetic.getPriority(loc7) <= Arithmetic.getPriority(loc1[(loc1.length - 1)]) && loc1.length) 
                    {
                        loc3 = loc1.pop();
                        loc4 = loc2.pop();
                        loc5 = loc2.pop();
                        loc2.push(Arithmetic.getValue(loc3, loc4, loc5));
                    }
                    loc1.push(arg1.charAt(loc6));
                    ++loc6;
                    continue;
                }
                loc8 = "";
                while (Arithmetic.isNumber(arg1, loc6) && loc6 < arg1.length) 
                {
                    loc8 = loc8 + arg1.charAt(loc6);
                    ++loc6;
                }
                loc2.push(Number(loc8));
            }
            while (loc1.length) 
            {
                loc3 = loc1.pop();
                loc4 = loc2.pop();
                loc5 = loc2.pop();
                loc2.push(Arithmetic.getValue(loc3, loc4, loc5));
            }
            return loc2.pop();
        }
    }
